package cn.iocoder.yudao.module.mqtt.service.devicelatestflowlog;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicelatestflow.DeviceLatestFlowDO;
import cn.iocoder.yudao.module.mqtt.util.LisTreeUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.mqtt.controller.admin.devicelatestflowlog.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicelatestflowlog.DeviceLatestFlowLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.mqtt.dal.mysql.devicelatestflowlog.DeviceLatestFlowLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.mqtt.enums.ErrorCodeConstants.*;

/**
 * 水量日志 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class DeviceLatestFlowLogServiceImpl implements DeviceLatestFlowLogService {

    @Resource
    private DeviceLatestFlowLogMapper deviceLatestFlowLogMapper;

    @Override
    public Long createDeviceLatestFlowLog(DeviceLatestFlowLogSaveReqVO createReqVO) {
        // 插入
        DeviceLatestFlowLogDO deviceLatestFlowLog = BeanUtils.toBean(createReqVO, DeviceLatestFlowLogDO.class);
        deviceLatestFlowLogMapper.insert(deviceLatestFlowLog);
        // 返回
        return deviceLatestFlowLog.getId();
    }

    @Override
    public Long createDeviceLatestFlowLog(DeviceLatestFlowDO waterFlowDO, DeviceLatestFlowDO sunFlowDO) {
        DeviceLatestFlowLogDO logDO = new DeviceLatestFlowLogDO();
        logDO.setGatewayId(waterFlowDO.getGatewayId());
        logDO.setDeviceName(waterFlowDO.getDeviceName());
        logDO.setDeviceId(waterFlowDO.getDeviceId());
        logDO.setDeviceKey(waterFlowDO.getDeviceKey());
        if(ObjectUtil.isNotNull(sunFlowDO)){
            logDO.setLisDay(waterFlowDO.getLisDay());
            logDO.setLisAll(waterFlowDO.getLisAll());
            logDO.setLisYear(waterFlowDO.getLisYear());
            logDO.setLisTime(waterFlowDO.getLisTime());
            logDO.setListMonth(waterFlowDO.getListMonth());
        }
        if(ObjectUtil.isNotNull(sunFlowDO)){
            logDO.setLisSunAll(sunFlowDO.getLisSun());
            logDO.setLisSun(sunFlowDO.getLisSun());
            logDO.setLisSunTime(sunFlowDO.getLisSunTime());
        }
        logDO.setUpdater("SYS");
        logDO.setUpdateTime(LocalDateTime.now());
        logDO.setCreator("SYS");
        logDO.setDeleted(false);
        logDO.setCreateTime(LocalDateTime.now());
        deviceLatestFlowLogMapper.insert(logDO);
        return logDO.getId();
    }

    @Override
    public void updateDeviceLatestFlowLog(DeviceLatestFlowLogSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceLatestFlowLogExists(updateReqVO.getId());
        // 更新
        DeviceLatestFlowLogDO updateObj = BeanUtils.toBean(updateReqVO, DeviceLatestFlowLogDO.class);
        deviceLatestFlowLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceLatestFlowLog(Long id) {
        // 校验存在
        validateDeviceLatestFlowLogExists(id);
        // 删除
        deviceLatestFlowLogMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceLatestFlowLogListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceLatestFlowLogExists(ids);
        // 删除
        deviceLatestFlowLogMapper.deleteByIds(ids);
        }

    private void validateDeviceLatestFlowLogExists(List<Long> ids) {
        List<DeviceLatestFlowLogDO> list = deviceLatestFlowLogMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_LATEST_FLOW_LOG_NOT_EXISTS);
        }
    }

    private void validateDeviceLatestFlowLogExists(Long id) {
        if (deviceLatestFlowLogMapper.selectById(id) == null) {
            throw exception(DEVICE_LATEST_FLOW_LOG_NOT_EXISTS);
        }
    }

    @Override
    public DeviceLatestFlowLogDO getDeviceLatestFlowLog(Long id) {
        return deviceLatestFlowLogMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceLatestFlowLogDO> getDeviceLatestFlowLogPage(DeviceLatestFlowLogPageReqVO pageReqVO) {
        return deviceLatestFlowLogMapper.selectPage(pageReqVO);
    }

}